package com.tianma315.web.procedures.controller;


import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


import com.tianma315.core.common.utils.Result;
import com.tianma315.core.oss.service.FileService;
import com.tianma315.core.procedures.domain.ProcedureLinkDO;
import com.tianma315.core.procedures.service.ProcedureLinkService;
import com.tianma315.core.procedures.vo.ProcedureLinkFileVO;
import com.tianma315.web.common.base.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;

;


/**
 *
 * <pre>
 * 环节
 * </pre>
 * <small> 2019-08-13 09:54:53 | Lgc</small>
 */
@Controller
@RequestMapping("/procedures/procedureLink")
public class ProcedureLinkController extends BaseController {
	@Autowired
	private ProcedureLinkService procedureLinkService;

	@Autowired
	private FileService fileService;

	/**
	 *
	 * @return 根据映射路径返回相应的页面
	 */
	@GetMapping()
	@RequiresPermissions("procedures:procedureLink:procedureLink")
	String ProcedureLink(){
		return "procedures/procedureLink/procedureLink";
	}


	@GetMapping("/query/{id}")
	//@RequiresPermissions("procedures:procedureLink:procedureLink")
	String ProcedureLinkById(@PathVariable("id") int id, Model model){
	model.addAttribute("id",id);
		return "procedures/procedureLink/procedureLink";
	}

	/**
	 *
	 * @param pageNumber 当前页数
	 * @param pageSize 页的大小
	 * @param procedureLinkDTO 实体类数据的模糊查询
	 * @return 返回一个Result对象封装了分页数据
	 */
	@ResponseBody
	@GetMapping("/list")
	//@RequiresPermissions("procedures:procedureLink:procedureLink")
	public Result<Page<ProcedureLinkFileVO>> list(Integer pageNumber, Integer pageSize, ProcedureLinkDO procedureLinkDTO){

//		procedureLinkDTO.setMerchantId(getMerchantId());
		// 查询列表数据
		Page<ProcedureLinkFileVO> page = procedureLinkService.getProcedureLinkDOPage(pageNumber,pageSize,procedureLinkDTO);
		return Result.ok(page);
	}
	/**
	 *
	 */
	@ResponseBody
	@GetMapping("/all")
	public List<ProcedureLinkFileVO> all(ProcedureLinkDO procedureLinkDO){
		// 查询列表数据
		List<ProcedureLinkFileVO> all = procedureLinkService.getProcedureLinkFileVO(procedureLinkDO.getProceduresId());
		return all;
	}

	/**
	 *
	 * @return 跳转到添加页面
	 */
	@GetMapping("/add/{proceduresId}")
	//@RequiresPermissions("procedures:procedureLink:add")
	String add(@PathVariable("proceduresId") Integer proceduresId,Model model){
		model.addAttribute("proceduresId",proceduresId);
	    return "procedures/procedureLink/add";
	}
	/**
	 *获取一级环节
	 */
	@ResponseBody
	@GetMapping("/first/{proceduresId}")
	public List<ProcedureLinkFileVO> first(@PathVariable("proceduresId") Integer proceduresId){
		// 查询列表数据
		List<ProcedureLinkFileVO> all = procedureLinkService.getProcedureLinkFileVO(proceduresId);
		List<ProcedureLinkFileVO> collect = all.stream().filter(s -> s.getType() == 0).collect(Collectors.toList());
		return collect;
	}

	/**
	 *
	 * @param procedureLinkId 根据id去查询要修改的数据
	 * @param model 将数据存放在model域中去
	 * @return 内部转发到编辑页并将数据渲染到页面中去
	 */
	@GetMapping("/edit/{procedureLinkId}")
	//@RequiresPermissions("procedures:procedureLink:edit")
	String edit(@PathVariable("procedureLinkId") Integer procedureLinkId,Model model){
		ProcedureLinkDO procedureLink = procedureLinkService.selectById(procedureLinkId);
		model.addAttribute("procedureLink", procedureLink);
	    return "procedures/procedureLink/edit";

	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	//@RequiresPermissions("procedures:procedureLink:add")
	public Result<String> save( ProcedureLinkFileVO procedureLinkFileVO){
//		procedureLinkFileVO.setMerchantId(getMerchantId());
		procedureLinkFileVO.setCreateBy(getUserId());
		List<ProcedureLinkDO> list =procedureLinkService.checkProcedureLinkSort(procedureLinkFileVO);
		if (list!=null && list.size()>0){
			return Result.fail("排序值不能重复",null);
		}
		if(procedureLinkService.addProcedureLink(procedureLinkFileVO)){
			return Result.ok();
		}
		return Result.fail();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	//@RequiresPermissions("procedures:procedureLink:edit")
	public Result<String>  update( ProcedureLinkFileVO procedureLinkFileVO){
		String url=null;
		try {
			if (!procedureLinkFileVO.getIconFile().isEmpty()){
				url = fileService.upload(procedureLinkFileVO.getIconFile().getBytes(),procedureLinkFileVO.getIconFile().getOriginalFilename());
				procedureLinkFileVO.setIcon(url);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		List<ProcedureLinkDO> list = procedureLinkService.checkProcedureLinkSort(procedureLinkFileVO);
		if (list!=null && list.size()>0){
			return Result.fail("排序值不能重复");
		}
		procedureLinkService.updateById(procedureLinkFileVO);
		return Result.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	//@RequiresPermissions("procedures:procedureLink:remove")
	public Result<String>  remove( Integer procedureLinkId){
		if(procedureLinkService.deleteProcedureLink(procedureLinkId)){
			return Result.ok();
		}
		return Result.fail();
	}

	/**
	 * 批量删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	//@RequiresPermissions("procedures:procedureLink:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] procedureLinkIds){
		for (Integer id:procedureLinkIds){
			procedureLinkService.deleteProcedureLink(id);
		}
		return Result.ok();
	}

}
